<!DOCTYPE html>
<html>
<head>
    <title>用户登录</title>
    <meta charset="UTF-8">
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 400px;
            margin: 50px auto;
            padding: 20px;
            background-color: #f0f0f0;
        }
        .form-container {
            background: white;
            padding: 25px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
        }
        .form-group {
            margin-bottom: 15px;
            position: relative;
        }
        label {
            display: block;
            margin-bottom: 5px;
            color: #333;
        }
        input {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 14px;
        }
        button {
            width: 100%;
            background-color: #4CAF50;
            color: white;
            padding: 12px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
            transition: background-color 0.3s;
        }
        button:hover {
            background-color: #45a049;
        }
        .link {
            text-align: center;
            margin-top: 20px;
        }
        .link a {
            color: #4CAF50;
            text-decoration: none;
        }
        .error {
            color: red;
            font-size: 0.9em;
            margin-top: 5px;
        }
    </style>
</head>
<body>
<div class="form-container">
    <h2>用户登录</h2>
    <form id="loginForm">
        <div class="form-group">
            <label>用户名：</label>
            <input type="text" id="username" name="username" required>
        </div>
        <div class="form-group">
            <label>密码：</label>
            <input type="password" id="password" name="password" required>
            <div class="error" id="passwordError"></div>
        </div>
        <button type="submit">登录</button>
    </form>

    <div class="link">
        <p>还没有账号？<a href="register.html">立即注册</a></p>
    </div>

    <div id="result"></div>
</div>

<script>
    // 注册成功后跳转逻辑（需在注册页面脚本中添加）
    // 在注册页面的注册成功回调中添加：
    // window.location.href = 'login.html'; // 示例注册页面跳转代码

    // 登录表单提交处理
    document.getElementById('loginForm').addEventListener('submit', function(e) {
        e.preventDefault();
        const username = document.getElementById('username').value;
        const password = document.getElementById('password').value;

        fetch('http://localhost:9100/api/users/login', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({
                username,
                password
            })
        })
            .then(response => {
                if (!response.ok) {
                    if (response.status === 401) {
                        throw new Error('用户名或密码错误');
                    }
                    throw new Error('登录失败：' + response.status);
                }
                return response.json();
            })
            .then(data => {
                // 登录成功逻辑（如跳转主页）
                window.location.href = 'home.html'; // 替换为实际主页地址
                document.getElementById('result').innerHTML =
                    `<p style="color: green;">登录成功！欢迎 ${data.username}</p>`;
            })
            .catch(error => {
                document.getElementById('result').innerHTML =
                    `<p style="color: red;">${error.message}</p>`;
            });
    });
</script>
</body>
</html>